package com.tz.init;

import com.tz.AppConfig;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by Administrator on 16-7-28.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class InitTables {

    @Autowired
    private LocalSessionFactoryBean localSessionFactoryBean;

    @Test
    public void createTables() {
        Configuration cfg = localSessionFactoryBean.getConfiguration();
        SchemaExport se = new SchemaExport(cfg);
//        第一个参:sql脚本是否输出到控制台
        //第二个参:sql脚本是否导入到数据库
        se.create(true, true);
    }
/*
"C:\Program Files\Java\jdk1.8.0_102\bin\java" -ea -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.2\bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.2\lib\idea_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_102\jre\lib\rt.jar;D:\maven\boot-shop-ng2\server(ssh)\target\test-classes;D:\maven\boot-shop-ng2\server(ssh)\target\classes;D:\mvn\repos\org\apache\struts\struts2-convention-plugin\2.3.30\struts2-convention-plugin-2.3.30.jar;D:\mvn\repos\org\apache\commons\commons-lang3\3.2\commons-lang3-3.2.jar;D:\mvn\repos\org\apache\struts\struts2-core\2.3.30\struts2-core-2.3.30.jar;D:\mvn\repos\org\apache\struts\xwork\xwork-core\2.3.30\xwork-core-2.3.30.jar;D:\mvn\repos\ognl\ognl\3.0.19\ognl-3.0.19.jar;D:\mvn\repos\asm\asm\3.3\asm-3.3.jar;D:\mvn\repos\asm\asm-commons\3.3\asm-commons-3.3.jar;D:\mvn\repos\asm\asm-tree\3.3\asm-tree-3.3.jar;D:\mvn\repos\org\freemarker\freemarker\2.3.22\freemarker-2.3.22.jar;D:\mvn\repos\javassist\javassist\3.11.0.GA\javassist-3.11.0.GA.jar;D:\mvn\repos\commons-fileupload\commons-fileupload\1.3.2\commons-fileupload-1.3.2.jar;D:\mvn\repos\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\mvn\repos\org\apache\struts\struts2-json-plugin\2.3.30\struts2-json-plugin-2.3.30.jar;D:\mvn\repos\org\apache\struts\struts2-spring-plugin\2.3.30\struts2-spring-plugin-2.3.30.jar;D:\mvn\repos\org\springframework\spring-context\4.3.2.RELEASE\spring-context-4.3.2.RELEASE.jar;D:\mvn\repos\org\springframework\spring-web\4.3.2.RELEASE\spring-web-4.3.2.RELEASE.jar;D:\mvn\repos\org\springframework\spring-aop\4.3.2.RELEASE\spring-aop-4.3.2.RELEASE.jar;D:\mvn\repos\org\springframework\spring-beans\4.3.2.RELEASE\spring-beans-4.3.2.RELEASE.jar;D:\mvn\repos\org\springframework\spring-core\4.3.2.RELEASE\spring-core-4.3.2.RELEASE.jar;D:\mvn\repos\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;D:\mvn\repos\org\springframework\spring-expression\4.3.2.RELEASE\spring-expression-4.3.2.RELEASE.jar;D:\mvn\repos\org\springframework\spring-aspects\4.3.2.RELEASE\spring-aspects-4.3.2.RELEASE.jar;D:\mvn\repos\org\aspectj\aspectjweaver\1.8.9\aspectjweaver-1.8.9.jar;D:\mvn\repos\org\springframework\spring-orm\4.3.2.RELEASE\spring-orm-4.3.2.RELEASE.jar;D:\mvn\repos\org\springframework\spring-jdbc\4.3.2.RELEASE\spring-jdbc-4.3.2.RELEASE.jar;D:\mvn\repos\org\springframework\spring-tx\4.3.2.RELEASE\spring-tx-4.3.2.RELEASE.jar;D:\mvn\repos\junit\junit\4.12\junit-4.12.jar;D:\mvn\repos\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\mvn\repos\org\springframework\spring-test\4.3.2.RELEASE\spring-test-4.3.2.RELEASE.jar;D:\mvn\repos\org\fluttercode\datafactory\datafactory\0.8\datafactory-0.8.jar;D:\mvn\repos\org\hibernate\hibernate-core\4.3.11.Final\hibernate-core-4.3.11.Final.jar;D:\mvn\repos\org\jboss\logging\jboss-logging\3.1.3.GA\jboss-logging-3.1.3.GA.jar;D:\mvn\repos\org\jboss\logging\jboss-logging-annotations\1.2.0.Beta1\jboss-logging-annotations-1.2.0.Beta1.jar;D:\mvn\repos\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.0.0.Final\jboss-transaction-api_1.2_spec-1.0.0.Final.jar;D:\mvn\repos\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;D:\mvn\repos\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;D:\mvn\repos\org\hibernate\common\hibernate-commons-annotations\4.0.5.Final\hibernate-commons-annotations-4.0.5.Final.jar;D:\mvn\repos\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;D:\mvn\repos\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;D:\mvn\repos\antlr\antlr\2.7.7\antlr-2.7.7.jar;D:\mvn\repos\org\jboss\jandex\1.1.0.Final\jandex-1.1.0.Final.jar;D:\mvn\repos\org\hibernate\hibernate-c3p0\4.3.11.Final\hibernate-c3p0-4.3.11.Final.jar;D:\mvn\repos\com\mchange\c3p0\0.9.2.1\c3p0-0.9.2.1.jar;D:\mvn\repos\com\mchange\mchange-commons-java\0.2.3.4\mchange-commons-java-0.2.3.4.jar;D:\mvn\repos\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar;D:\mvn\repos\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;D:\mvn\repos\commons-pool\commons-pool\1.5.4\commons-pool-1.5.4.jar;D:\mvn\repos\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\mvn\repos\jstl\jstl\1.2\jstl-1.2.jar;D:\mvn\repos\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;D:\mvn\repos\org\apache\tomcat\tomcat-catalina\7.0.70\tomcat-catalina-7.0.70.jar;D:\mvn\repos\org\apache\tomcat\tomcat-servlet-api\7.0.70\tomcat-servlet-api-7.0.70.jar;D:\mvn\repos\org\apache\tomcat\tomcat-juli\7.0.70\tomcat-juli-7.0.70.jar;D:\mvn\repos\org\apache\tomcat\tomcat-annotations-api\7.0.70\tomcat-annotations-api-7.0.70.jar;D:\mvn\repos\org\apache\tomcat\tomcat-api\7.0.70\tomcat-api-7.0.70.jar;D:\mvn\repos\org\apache\tomcat\tomcat-util\7.0.70\tomcat-util-7.0.70.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.tz.init.InitTables,createTables

    alter table SHOP_ADDRESS
            drop
    foreign key FK_48s9m4g3sxwubidbyvxn4aw16

    alter table SHOP_CATEGORY
            drop
    foreign key FK_55r8jwkmeadngua1k30rtb359

    alter table SHOP_PICTURE
            drop
    foreign key FK_el20tmbeg9xus27buhj306k1x

    alter table SHOP_PRODUCT
            drop
    foreign key FK_fvfnqa95c63aphnfgstdmieyb

    drop table if exists SHOP_ADDRESS

    drop table if exists SHOP_CATEGORY

    drop table if exists SHOP_PICTURE

    drop table if exists SHOP_PRODUCT

    drop table if exists SHOP_USER

    create table SHOP_ADDRESS (
            id varchar(36) not null,
    create_date datetime,
    modify_date datetime,
    version integer,
    additionalInformation varchar(255),
    addressLine1 varchar(255),
    addressLine2 varchar(255),
    city varchar(255),
    company varchar(255),
    firstname varchar(255),
    homePhone varchar(255),
    lastname varchar(255),
    mobilePhone varchar(255),
    postcode varchar(255),
    USER_ID varchar(36) not null,
    primary key (id)
    )

    create table SHOP_CATEGORY (
            id varchar(36) not null,
    create_date datetime,
    modify_date datetime,
    version integer,
    c_level integer,
    name varchar(255),
    c_remark varchar(255),
    c_seq integer,
    category_id varchar(36),
    primary key (id)
    )

    create table SHOP_PICTURE (
            id varchar(36) not null,
    create_date datetime,
    modify_date datetime,
    version integer,
    cover bit not null,
    path varchar(255),
    product_id varchar(36),
    primary key (id)
    )

    create table SHOP_PRODUCT (
            id varchar(36) not null,
    create_date datetime,
    modify_date datetime,
    version integer,
    p_color varchar(255),
    p_desc longtext,
    p_detail varchar(255),
    p_discount double precision,
    p_is_featured bit,
    p_is_latest bit,
    p_name varchar(255),
    p_price double precision,
    p_is_sidebar bit,
    p_stock integer,
    p_subName varchar(255),
    p_tax double precision,
    category_id varchar(36),
    primary key (id)
    )

    create table SHOP_USER (
            id varchar(36) not null,
    create_date datetime,
    modify_date datetime,
    version integer,
    birthday varchar(255),
    email varchar(255),
    firstname varchar(255),
    lastname varchar(255),
    password varchar(255),
    title varchar(255),
    primary key (id)
    )

    alter table SHOP_ADDRESS
    add constraint FK_48s9m4g3sxwubidbyvxn4aw16
    foreign key (USER_ID)
    references SHOP_USER (id)

    alter table SHOP_CATEGORY
    add constraint FK_55r8jwkmeadngua1k30rtb359
    foreign key (category_id)
    references SHOP_CATEGORY (id)

    alter table SHOP_PICTURE
    add constraint FK_el20tmbeg9xus27buhj306k1x
    foreign key (product_id)
    references SHOP_PRODUCT (id)

    alter table SHOP_PRODUCT
    add constraint FK_fvfnqa95c63aphnfgstdmieyb
    foreign key (category_id)
    references SHOP_CATEGORY (id)

    Process finished with exit code 0
*/

}
